const { marked } = require('marked');

/**
 * Markdown增强插件
 * 提供额外的Markdown渲染功能
 */
class MarkdownEnhancerPlugin {
  constructor() {
    this.name = 'markdown-enhancer';
    this.version = '1.0.0';
    this.description = 'Enhances Markdown rendering with additional features';
  }

  /**
   * 激活插件
   * @param {Object} context - 插件上下文
   */
  async activate(context) {
    console.log('Markdown Enhancer plugin activated');
    
    // 添加自定义渲染器
    const renderer = new marked.Renderer();
    
    // 增强表格渲染
    renderer.table = (header, body) => {
      return `
        <div class="table-wrapper">
          <table class="markdown-table">
            <thead>${header}</thead>
            <tbody>${body}</tbody>
          </table>
        </div>
      `;
    };
    
    // 增强代码块渲染
    renderer.code = (code, infostring, escaped) => {
      const lang = (infostring || '').match(/\S*/)[0];
      
      if (lang === 'mermaid') {
        return `<div class="mermaid">${code}</div>`;
      }
      
      return false; // 使用默认渲染器
    };
    
    marked.use({ renderer });
    
    // 在全局上下文中暴露增强的marked
    if (context && context.expose) {
      context.expose('marked', marked);
    }
    
    return { marked };
  }

  /**
   * 停用插件
   */
  async deactivate() {
    console.log('Markdown Enhancer plugin deactivated');
  }
}

module.exports = MarkdownEnhancerPlugin;